/**
 * @ClassName: UserServiceImpl
 * $
 * @Date: 2025/4/19 17:02
 */


package com.itheima.mp.service.Impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.mapper.UserMapper;
import com.itheima.mp.service.IUserService;
import org.springframework.stereotype.Service;


import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {


    @Override
    public void deductionBalance(Long id, Long money) {
        //1.查询用户
        User user = this.getById(id);
        //2.校验用户状态
        if(user==null||user.getStatus()==2){
            throw new RuntimeException("用户状态异常");
        }
        //3.校验余额是否充足
        if(user.getBalance()<money){
            throw new RuntimeException("用户余额不足");
        }
        //4.扣减余额
        lambdaUpdate()
                .set(User::getBalance,user.getBalance()-money)
                .set((user.getBalance()-money)==0,User::getStatus,2)
        .eq(User::getId,id)
        .update();
    }

    @Override
    public List<User> queryUsers(String name, Integer status, Integer minBalance, Integer maxBalance) {
        List<User> list = lambdaQuery()
                .like(name != null, User::getUsername, name)
                .eq(status != null, User::getStatus, status)
                .ge(minBalance != null, User::getBalance, minBalance)
                .le(maxBalance != null, User::getBalance, minBalance)
                .list();
        return list;

    }
}
